package tools;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author 路老师
 * @date 2025/3/25
 * 无精神，不编程！无乐趣，不编程！
 * “调试：与bug共舞的艺术。”
 */
public class ThingManage {
    public static void startThing() {
        try {
            //输出查询过程日志信息
            Properties properties = new Properties();
            FileInputStream fileInputStream = new FileInputStream("src/main/resources/config/log4j.properties");
            properties.load(fileInputStream);
            PropertyConfigurator.configure(properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession() {

        SqlSession sqlSession = null;
        try {
            //1.读取mybatis配置文件
            InputStream resourceAsStream = Resources.getResourceAsStream("config/mybatis-config0.xml");
            //2.获取sqlSession工厂对象
            SqlSessionFactory buildFac = new SqlSessionFactoryBuilder().build(resourceAsStream);
            //3.通过工厂获取对象
            sqlSession = buildFac.openSession();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSession;
    }
}
